﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sengoku
{
    class Sengoku2Game
    {
        private Board board = new Board(12, 12);

        public void populate()
        {
            //lowercase-high
            board.addPieceAt(new BetaPiece(0), 1, 4);
            board.addPieceAt(new GammaPiece(0), 1, 5);
            board.addPieceAt(new GammaPiece(0), 1, 6);
            board.addPieceAt(new BetaPiece(0), 1, 7);
            board.addPieceAt(new DeltaPiece(0), 2, 4);
            board.addPieceAt(new DeltaPiece(0), 2, 5);
            board.addPieceAt(new DeltaPiece(0), 2, 6);
            board.addPieceAt(new DeltaPiece(0), 2, 7);

            //lowercase-low
            board.addPieceAt(new TauPiece(0), 4, 9);
            board.addPieceAt(new TauPiece(0), 5, 9);
            board.addPieceAt(new TauPiece(0), 6, 9);
            board.addPieceAt(new TauPiece(0), 7, 9);
            board.addPieceAt(new SigmaPiece(0), 4, 10);
            board.addPieceAt(new UpsilonPiece(0), 5, 10);
            board.addPieceAt(new UpsilonPiece(0), 6, 10);
            board.addPieceAt(new SigmaPiece(0), 7, 10);

            //uppercase-low
            board.addPieceAt(new SigmaPiece(1), 4, 1);
            board.addPieceAt(new UpsilonPiece(1), 5, 1);
            board.addPieceAt(new UpsilonPiece(1), 6, 1);
            board.addPieceAt(new SigmaPiece(1), 7, 1);
            board.addPieceAt(new TauPiece(1), 4, 2);
            board.addPieceAt(new TauPiece(1), 5, 2);
            board.addPieceAt(new TauPiece(1), 6, 2);
            board.addPieceAt(new TauPiece(1), 7, 2);

            //uppercase-high
            board.addPieceAt(new DeltaPiece(1), 9, 4);
            board.addPieceAt(new DeltaPiece(1), 9, 5);
            board.addPieceAt(new DeltaPiece(1), 9, 6);
            board.addPieceAt(new DeltaPiece(1), 9, 7);
            board.addPieceAt(new BetaPiece(1), 10, 4);
            board.addPieceAt(new GammaPiece(1), 10, 5);
            board.addPieceAt(new GammaPiece(1), 10, 6);
            board.addPieceAt(new BetaPiece(1), 10, 7);

            printBoard();
        }

        public void playGame()
        {
            bool uppercaseHighWin = false;
            bool uppercaseLowWin = false;
            bool lowercaseHighWin = false;
            bool lowercaseLowWin = false;
            int turnCounter = 400;

            while (!uppercaseHighWin && !uppercaseLowWin && !lowercaseLowWin && !lowercaseHighWin && turnCounter > 0)
            {
                turnCounter--;

                takeTurn(1, 0);
                printBoard();
                uppercaseLowWin = winCheck(1, 0);

                takeTurn(0, 1);
                printBoard();
                uppercaseLowWin = winCheck(1, 0);

                takeTurn(0, 0);
                printBoard();
                uppercaseLowWin = winCheck(1, 0);

                takeTurn(1, 1);
                printBoard();
                uppercaseLowWin = winCheck(1, 0);
            }
        }

        private bool winCheck(int letterCase, int letterHighOrLow)
        {
            int enemyCount = 0;

            for (int i = 0; i < board.getColumns(); i++)
                for (int j = 0; j < board.getRows(); j++)
                    if (board.getPieceAt(i, j) != null && (((board.getPieceAt(i, j)).getCase() != letterCase) || ((board.getPieceAt(i, j)).getHighOrLow() != letterHighOrLow))) //looks for enemies, then increments a counter for each one found
                        enemyCount++;

            if (enemyCount > 0)//if any enemies are found for the player checking, the game is not over
                return false;
            else
                return true;
        }

        public void takeTurn(int letterCase, int letterHighOrLow)
        {
            bool turnOver = false;
            Console.WriteLine();

            if (letterCase == 1)
            {
                if (letterHighOrLow == 0)
                    Console.WriteLine("Player One (uppercase-low), choose the coordinates of a piece to move.");
                else
                    Console.WriteLine("Player Four (uppercase-high), choose the coordinates of a piece to move.");
            }
            else
            {
                if (letterHighOrLow == 1)
                    Console.WriteLine("Player Two (lowercase-high), choose the coordinates of a piece to move.");
                else
                    Console.WriteLine("Player Three (lowercase-low), choose the coordinates of a piece to move.");
            }
            
            while (!turnOver)
            {
                Console.Write("Column:  ");
                int c = Int32.Parse(Console.ReadLine());
                Console.Write("Row: ");
                int r = Int32.Parse(Console.ReadLine());
                Position pos = new Position(c, r);

                if ((board.getPieceAt(pos.getC(), pos.getR()).getCase()) == letterCase && (board.getPieceAt(pos.getC(), pos.getR()).getHighOrLow()) == letterHighOrLow)
                {
                    Position newPos = (board.getPieceAt(pos.getC(), pos.getR())).move();
                    board.addPieceAt((board.getPieceAt(pos.getC(), pos.getR())), newPos.getC(), newPos.getR());
                    board.removePieceAt(pos.getC(), pos.getR());
                    turnOver = true;
                }
                else
                    Console.WriteLine("Invalid");
            }
        }

        public void printBoard()
        {
            Console.WriteLine();
            
            Console.WriteLine();

            for (int i = 0; i < board.getColumns(); i++)
            {
                
                for (int j = 0; j < board.getRows(); j++)
                {
                    if (board.getPieceAt(i, j) != null)
                    {
                        Console.Write((board.getPieceAt(i, j)).getLetter() + "    ");
                    }
                    else
                    {
                        Console.Write("-    ");
                    }
                }
                Console.Write(i + " ");
                Console.WriteLine();
            }

            for (int j = 0; j < board.getRows(); j++)
                Console.Write(j + "    ");
            
            Console.WriteLine();
        }
    }
}
